package com.example.cs342messenger;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import basicClasses.LoginObject;

import com.example.cs342messenger.LoginActivity.UserLoginTask;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class Register extends Activity
{
	SharedPreferences	mPrefs;
	LoginObject			login;
	UserRegisterTask	register;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_register, menu);
		return true;
	}

	public void reg(View view)
	{
		EditText u = (EditText) findViewById(R.id.uName);
		EditText p = (EditText) findViewById(R.id.pWord);
		EditText p2 = (EditText) findViewById(R.id.pWord2);
		String user = u.getText().toString();
		String pass = p.getText().toString();
		String pass2 = p.getText().toString();

		if (pass.equals(pass2))
		{
			// code that is incomplete, but works for registering
			login = new LoginObject(user, pass);
			register = new UserRegisterTask();
			register.execute(login);

			mPrefs = getSharedPreferences("myPrefs", MODE_PRIVATE);
			SharedPreferences.Editor edit = mPrefs.edit();
			edit.putString("user", user);
			edit.commit();

			Toast.makeText(this, "Register Successful", Toast.LENGTH_SHORT)
					.show();
			LoginActivity.registerAcc = true;
			finish();
		}
		else
		{

		}
	}

	public class UserRegisterTask extends AsyncTask<LoginObject, Void, Boolean>
	{

		@Override
		protected Boolean doInBackground(LoginObject... params)
		{
			// TODO: attempt authentication against a network service.
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(
					"http://softeng.cs.uwosh.edu/students/cs342g6/sign_up.php");
			Boolean response = false;
			try
			{
				// Convert the login object to XML
				XStream xstream = new XStream(new DomDriver("UTF-8"));
				xstream.alias("Login", LoginObject.class);
				String xml = xstream.toXML(login);

				// Pass the XML as a StringEntity
				StringEntity se = new StringEntity(xml, HTTP.UTF_8);
				se.setContentType("text/xml");
				httppost.setEntity(se);
				System.out.println("MADE IT TO RESPONSE");
				HttpResponse httpresponse = httpclient.execute(httppost);
				HttpEntity resEntity = httpresponse.getEntity();
				String resp = EntityUtils.toString(resEntity);
				System.out.println(resp);
				response = convertToBool(resp);
				if (response)
					System.out.println("true");
				else
					System.out.println("false");

			}
			catch (ClientProtocolException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return response;
		}

		@Override
		protected void onPostExecute(Boolean success)
		{
			register = null;
			if (success)
			{
				Intent intent = new Intent(Register.this,LoginActivity.class);
				Register.this.startActivity(intent);
				finish();
			}
			else
			{
				//Make a toast that complains
				System.out.println("failure");
			}
		}
		
		/*
		 * Convert the string return from the web service to a boolean
		 */
		public boolean convertToBool(String str)
		{
			if (str.equalsIgnoreCase("TRUE"))
				return true;
			else
				return false;
		}

	}

}
